import ast
# import os
import re

from commond import read_excel, read_config
from commond.config_http import RunMain


# from commond.get_path_info import get_test_project

# url = read_config.ReadConfig('config.ini', 'WP').get_url()
# login_xls = read_excel.ReadExcel().get_xls('WP.xlsx', 'login')


class GetCookie(object):
    def __init__(self, login_ls):
        login_info = login_ls[0]
        self.case_name = login_info[0]
        self.path = login_info[1]
        self.query = login_info[2]
        self.method = login_info[3]
        self.query_format = login_info[4]
        if self.query == "":
            self.query = self.query
        else:
            self.query = ast.literal_eval(self.query)

    def get_cookie(self, config, project):
        url = read_config.ReadConfig(config, project).get_url()
        query_format = self.query_format
        nwe_url = url + self.path
        response = RunMain(config, project).run_main(self.method, nwe_url, data=self.query,
                                                     query_format=query_format)

        if re.search('right', self.case_name):
            assert response.json()['success'] is True
            cookie = response.headers['Set-Cookie']
            # print(response.text)
            return cookie

    def set_headers(self, config, project):
        headers = {
            "Cookie": self.get_cookie(config, project)
        }
        return headers


if __name__ == "__main__":
    # project_name = get_test_project("WP")
    # config_name = os.path.join(project_name, "testfile", "config.ini")
    # print(config_name)
    # print(project_name)
    login_xls = read_excel.ReadExcel("WP").get_xls('WP.xlsx', 'login')

    coo = GetCookie(login_xls)
    # coo.get_cookie()
    print(coo.get_cookie("config.ini", "WP"))
    # print(coo.set_headers())
